package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.QuestionCreateDTO;
import edu.sdjzu.exam.dto.QuestionDTO;
import edu.sdjzu.exam.dto.QuestionUpdateDTO;
import edu.sdjzu.exam.service.question.QuestionService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("teacher")
public class QuestionController {

    private final QuestionService questionService;

    @PostMapping("/create")
    public CommonResponse<Long> createQuestion(@RequestBody QuestionCreateDTO createDTO) {
        try {
            Long questionId = questionService.createQuestion(createDTO);
            return CommonResponse.success("题目创建成功", questionId);
        } catch (Exception e) {
            return CommonResponse.error("创建失败：" + e.getMessage());
        }
    }

    @GetMapping("/{questionId}")
    public CommonResponse<QuestionDTO> getQuestion(@PathVariable Long questionId) {
        try {
            QuestionDTO questionDTO = questionService.getQuestion(questionId);
            return CommonResponse.success(questionDTO);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @PutMapping("/{questionId}")
    public CommonResponse<String> updateQuestion(@PathVariable Long questionId, @RequestBody QuestionUpdateDTO updateDTO) {
        try {
            updateDTO.setQuestionId(questionId); // 确保ID一致
            questionService.updateQuestion(updateDTO);
            return CommonResponse.success("题目更新成功");
        } catch (Exception e) {
            return CommonResponse.error("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{questionId}")
    public CommonResponse<String> deleteQuestion(@PathVariable Long questionId) {
        try {
            questionService.deleteQuestion(questionId);
            return CommonResponse.success("题目删除成功");
        } catch (Exception e) {
            return CommonResponse.error("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/my")
    public CommonResponse<List<QuestionDTO>> listMyQuestions() {
        try {
            List<QuestionDTO> questions = questionService.listQuestionsByCreator();
            return CommonResponse.success(questions);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/course/{courseId}")
    public CommonResponse<List<QuestionDTO>> listQuestionsByCourse(@PathVariable Long courseId) {
        try {
            List<QuestionDTO> questions = questionService.listQuestionsByCourse(courseId);
            return CommonResponse.success(questions);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    // 内部 DTO 类，用于接收 questionId
    @Data
    public static class QuestionIdDTO {
        private Long questionId;
    }
}